package com.briup.labs.controller;

import com.briup.labs.pojo.*;
import com.briup.labs.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/all")
    public Result<PageBean<SysUser>> all(String pageNum, String pageSize) {
        PageBean<SysUser> pageBean = sysUserService.all(pageNum, pageSize);
        if (pageBean.getTotal() != null && pageBean.getItems() != null) {
            return Result.success(pageBean);
        } else {
            return Result.error("输出传入格式错误");
        }
    }

    @GetMapping("/allUser")
    public Result<List<SysUser>> all() {
        List<SysUser> list = sysUserService.allUser();
        return Result.success(list);
    }

    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody User user) {
        //输入sysUser数据
        SysUser sysUser = new SysUser();
        sysUser.setUserId(user.getUserId());
        sysUser.setUsername(user.getUsername());
        sysUser.setRealname(user.getRealname());
        sysUser.setPassword(user.getPassword());
        sysUser.setEmail(user.getEmail());
        sysUser.setMobile(user.getMobile());
        sysUser.setStatus(user.getStatus());
        sysUser.setCreateUserId(user.getCreateUserId());
        sysUser.setCreateTime(user.getCreateTime());
        //输入sysRole数据
        SysRole sysRole = user.getSysRole();
        sysUserService.saveOrUpdate(sysUser, sysRole);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        sysUserService.delete(id);
        return Result.success();
    }
}
